package com.bsoft.wx.aop;

import com.bsoft.wx.config.shiro.ThreadLocalToken;
import com.bsoft.wx.util.R;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: emos-wx-api
 * @description: 切面类
 * @author: hdh
 * @create: 2023:02:02 15:24
 */

@Aspect
@Component
public class TokenAspect {
    @Autowired
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * com.bsoft.wx.controller.*.*(..)))")
    public void aspect() {
        System.out.println("切点");
    }

    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        R r = (R) point.proceed(); //方法执行
        String token = threadLocalToken.getToken();
        //ThreadLocal中存在token 说明是更新的token
        if (token != null) {
            r.put("token", token); //往响应中放置 token
            threadLocalToken.clear();
        }
        return r;
    }
}
